﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZK.CoalYardTransManage.DBUtility;
using ZK.Common;
using ZK.Common.ProModels;
using ZK.TransportManage.DBUtility;

namespace ZK.CoalYardTransManage
{
    public partial class MainForm : Form
    {

        //监听登录timer
        private System.Timers.Timer autoLoginTimer = null;
        private WFAutoSizeHelper wFAutoSizeHelper = new WFAutoSizeHelper();
        public bool InitName = true;
        public MainForm()
        {
            InitializeComponent();
            wFAutoSizeHelper.GetAllInitInfo(this.Controls[0], this);
            //GetAllInitInfo(this.Controls[0]);
            SetUsers();
        }
        /// <summary>
        /// 设置用户名称
        /// </summary>
        public void SetUsers()
        {
            string[] strArrYhz = AppConfigHelper.GetAppSettings("yhz").Split(',');
            for (int i = 0; i < strArrYhz.Length; i++)
            {
                comboBoxYhz.Items.Add(strArrYhz[i].Trim());
            }
        }
        /// <summary>
        /// 登录按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click_1(object sender, EventArgs e)
        {
            #region 登录验证
            string errorStr = string.Empty;
            if (string.IsNullOrEmpty(comboBoxYhz.Text.Trim()))
            {
                MessageBox.Show("用户名不能为空", "登录", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (string.IsNullOrEmpty(txtUserPwd.Text.Trim()))
            {
                MessageBox.Show("密码不能为空", "登录", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            ApiResponseDto loginDto = WebApiService.Login(comboBoxYhz.Text.Trim(), txtUserPwd.Text.Trim(), out errorStr);
            if (!string.IsNullOrEmpty(errorStr) || !loginDto.Success)
            {
                MessageBox.Show(loginDto.Msg, "登录", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtUserPwd.Text = "";
                return;
            }
            #endregion

            #region 保存指定节点的参数并刷新确保下次取到数据的及时性
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            BasicSetup setup = config.GetSection("BasicSetup") as BasicSetup;
            setup.UserInfo.UserName = comboBoxYhz.Text.Trim();
            AppConfigHelper.userName = comboBoxYhz.Text.Trim();
            setup.UserInfo.UserPwd = txtUserPwd.Text.Trim();
            config.SaveAs("App.config");
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("BasicSetup");
            #endregion

            //var a = DbHelperSQLite.Query("select * from test1");
            #region 跳转页面
            //Test.Led硬件单测 removalAndWeigh = new Test.Led硬件单测();
            this.Hide();
            if (InitName)
            {
                RemovalAndWeigh removalAndWeigh = new RemovalAndWeigh();
                removalAndWeigh.Show();
            }
            #endregion

        }
        /// <summary>
        /// enter 登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Login_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) //判断回车键
            {
                button1_Click_1(sender, e);
            }
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
        }

        private void MainForm_SizeChanged(object sender, EventArgs e)
        {
            if (wFAutoSizeHelper.ControlsInfo.Count > 0)//如果字典中有数据，即窗体改变
            {
                wFAutoSizeHelper.ControlsChangeInit(this.Controls[0]);//表示pannel控件
                wFAutoSizeHelper.ControlsChange(this.Controls[0]);

            }
        }

    }
}
